package com.biao.java8.demo;

import com.biao.java8.demo.pojo.Employee;

import java.util.*;

public class TestLambda {
    public static void main(String[] args) {

        // 原来的匿名内部类
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };
        TreeSet<Integer> integers = new TreeSet<>(comparator);

        // 使用lambda
        Comparator<Integer> comparator2 = (x, y) -> Integer.compare(x, y);
        TreeSet<Integer> integers2 = new TreeSet<>(comparator2);


        // 获取公司中员工年龄大于35 和 公司大于3000 的员工信息
        List<Employee> employees = Arrays.asList(
                new Employee("张三", 18, 1111.11),
                new Employee("李四", 28, 2222.22),
                new Employee("王五", 18, 3333.33),
                new Employee("赵六", 48, 4444.44),
                new Employee("田七", 58, 5555.55)
        );
        ArrayList<Employee> arrayList = new ArrayList<>();
        for (Employee employee : employees) {
            if (employee.getAge() > 35 && employee.getSalary() > 3000) {
                arrayList.add(employee);
            }
        }
        System.out.println(arrayList.toString());
        System.out.println("---------------------------------------------------------");

        // 使用lambda
        employees.stream().filter(employee -> employee.getAge() > 35 && employee.getSalary() > 3000)
                .map(Employee::getAge)     // 光打印名字
                .sorted((e, e2) -> Integer.compare(e2, e))     // 年龄倒排序,只有一行,可以不写大括号和return
                .limit(1)                   // 可以只取出e1个
                .forEach(System.out::println);      // 打印满足条件年龄最大的

        // Runnable 使用lambda
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("hello world");
            }
        };
        runnable.run();
        System.out.println("--------------------------------------------");
        // 使用lambda
        Runnable runnable2 = () -> System.out.println("hello lambda");
        runnable2.run();



        // 练习
        Collections.sort(employees, (e1,e2)->{
            if (e1.getAge() == e2.getAge()){
                return e1.getName().compareTo(e2.getName());        // 年龄一样比姓名
            }else {
                return -Integer.compare(e1.getAge(),e2.getAge());   // 按年龄倒排
            }
        });
        System.out.println("------------------------------");
        employees.forEach(System.out::println);
    }
}
